package DataStructure;

public class Sort {
    public static void insertSort(int[] array) {
        for (int i = 1; i <array.length ; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j > 0; j--) {
                if(array[j]>tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public static void shellSort(int[] array) {
        int gap = array.length;
        while(gap > 1) {
            gap = gap/2;
            shell(array,gap);
        }
     }
     private static void shell(int[] array,int gap) {
         for (int i = gap; i < array.length; i++) {
             int tmp = array[i];
             int j = i-gap;
             for ( ;j >= 0 ; j-=gap) {
                 if (array[j] > tmp) {
                     array[j+gap] = array[j];
                 }else{
                     break;
                 }
             } array[j+gap] = tmp;
         }
     }
     public void quickSort(int[] array) {

     }
     public static void bubbleSort(int[] array) {
         for (int i = 0; i < array.length-1; i++) {
             for (int j = i+1; j < array.length-1; j++) {
                 boolean flg = true;
                 if(array[j] > array[j+1]) {
                     swap(array,j,j+1);
                     flg = false;
                 }
                 //若没有发生交换，代表数组有序
                 if(flg) {
                     return;
                 }
             }
         }
     }
     private static void swap(int[] array,int i, int j) {
        int tmp = 0;
        tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
     }
}
